package com.ihr360.payroll.util;

import com.google.common.collect.Lists;
import com.ihr360.commons.lang.CollectionUtils;
import com.ihr360.commons.lang.StringUtils;
import com.ihr360.shared.basicstaffinfo.dto.SyncedBasicStaffInfo;
import com.ihr360.type.StaffStatus;
import org.apache.commons.collections.MapUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Eric on 2018/4/28/028.
 */
public class StaffUtils {

    /**
     * 获取员工姓名+号码 map
     */
    public static Map<String, List<SyncedBasicStaffInfo>> getStaffMap(List<SyncedBasicStaffInfo> staffList) {
        Map<String, List<SyncedBasicStaffInfo>> staffMap = new HashMap<>();
        if (CollectionUtils.isEmpty(staffList)) {
            return staffMap;
        }
        for (SyncedBasicStaffInfo staff : staffList) {
            String key = staff.getStaffName() + staff.getMobileNo();
            List<SyncedBasicStaffInfo> tempList = staffMap.get(key);
            if (CollectionUtils.isEmpty(tempList)) {
                tempList = Lists.newArrayList();
            }
            tempList.add(staff);
            staffMap.put(key, tempList);
        }
        return staffMap;
    }

    /**
     * 根据姓名号码获取员工
     */
    public static SyncedBasicStaffInfo getImportStaff(Map<String, List<SyncedBasicStaffInfo>> staffMap, String staffName, String staffMobile) {
        if (MapUtils.isEmpty(staffMap) || StringUtils.isEmpty(staffName) || StringUtils.isEmpty(staffMobile)) {
            return null;
        }
        SyncedBasicStaffInfo staff = null;
        String key = staffName + staffMobile;
        if (!staffMap.containsKey(key)) {
            return null;
        }
        List<SyncedBasicStaffInfo> staffList = staffMap.get(key);
        if (CollectionUtils.isEmpty(staffList)) {
            return null;
        } else if (staffList.size() == 1) {
            staff = staffList.get(0);
        } else {
            //优先获取在职人员
            List<SyncedBasicStaffInfo> quitStaffList = Lists.newArrayList();
            for (SyncedBasicStaffInfo staffInfo : staffList) {
                if (StaffStatus.IN_SERVICE.equals(staffInfo.getStaffStatus())) {
                    staff = staffInfo;
                    break;
                }
                quitStaffList.add(staffInfo);
            }
            //在职人员未获取到，获取离职人员
            if (staff == null && CollectionUtils.isNotEmpty(quitStaffList)) {
                staff = quitStaffList.get(0);
            }
        }
        return staff;
    }
}
